import { Component, ViewChild } from '@angular/core';
import { NavParams, Events, AlertController, App, ModalController, NavController, Content, ActionSheetController, LoadingController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { Config } from '../../../providers/config';
import { DomSanitizer } from '@angular/platform-browser';
import _ from 'lodash';
// import * as Highcharts from 'highcharts';

import { Common } from "../../../providers/common";
import {BcwHomeService} from "./home.service";

@Component({
  selector: 'page-bcw-home-detail-pdf',
  templateUrl: './pdf.html'
})
export class BcwHomeDetailPdfPage {
	moa_page_name: any = "page-bcw-home-detail-pdf";
  moa_page_explain: any = "查看PDF文件";

  pdfLink = "";

  loading: any;

  zoom = 1;

  constructor(
    public alertCtrl: AlertController,
    public app: App,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public config: Config,
    private sanitizer: DomSanitizer,
    public events: Events,
    public storage: Storage,
    private common: Common,
    private actionSheetCtrl: ActionSheetController,
    private navParams: NavParams,
    public loadingCtrl: LoadingController
  ) {

  }

  ionViewDidEnter() {
    this.loading = this.loadingCtrl.create({
      content: '正在加载文档内容,请稍候...<br/>已完成：<span class="gfoa-load-pdf-percentage">0%</span>'
    });
    if (this.config.isProduct) {
      this.pdfLink = this.navParams.get("url");
    } else {
      this.pdfLink = "http://58.248.27.170:8082/bcwpdf/556588542313.pdf?isBCWPDF=true";
    }
    if (this.pdfLink) {
      this.loading.present();
    }

    console.log(this.pdfLink);
  }

  /**
   * 加载文档完成回调
   * @param
   */
  pdfLoadCallBackFn($event) {
    this.loading.dismiss();
  }

  /**
   * 加载文档出错回调
   * @param
   */
  onPdfLoadError($event) {
    this.pdfLoadCallBackFn(null);
    let alert = this.alertCtrl.create({
      title: '提示',
      subTitle: '文档内容加载失败,请稍候重试!',
      buttons: ['确认']
    });
    alert.present();
  }

  /**
   * 加载文档进度
   * @param progressData
   */
  onLoadPdfProgress(progressData) {
    var p = ((progressData.loaded / progressData.total) * 100).toFixed(2) + "%";
    document.getElementsByClassName("gfoa-load-pdf-percentage")[0].innerHTML = p;
  }

  /**
   * pdf显示放大缩小
   * @param type
   */
  zoomPdf(type) {
    console.log(type);
    if (type == 'add') {
      if (this.zoom > 0.4) {
        this.zoom = this.zoom - 0.2;
      }
    } else {
      if (this.zoom < 1) {
        this.zoom = this.zoom + 0.2;
      }
    }
  }
}
